package cn.imvc.app.controllers.member;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.Withdrawals;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.WithdrawalsModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

//TODO wujx
public class WithdrawalsControllerImpl implements WithdrawalsController{
	static Logger logger = Logger.getLogger(WithdrawalsControllerImpl.class);
	public Page<Withdrawals> page;
	public ViewRequest viewRequest;
	private int id;
	//驳回的回复内容
	private Withdrawals withdrawals;
	
	@Autowired
	LoggingModel loggingModel;
	
	@Autowired
	private WithdrawalsModel withdrawalsModel;
	
	public String search() {
		// TODO Auto-generated method stub
		return null;
	}

	public String index() {
		if (viewRequest == null) {
			viewRequest = new ViewRequest();
			
			// 获取当前日期
			try {
				Date date = new Date();
				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
				String nowDate = sf.format(date);

				// 获取明天
				Calendar cal = Calendar.getInstance();
				cal.setTime(sf.parse(nowDate));
				cal.add(Calendar.DAY_OF_YEAR, +1);
				String nextDay = sf.format(cal.getTime());

				viewRequest.getParams().put("_and_w_dot_createdAt_ge_date", nowDate);
				viewRequest.getParams().put("_and_w_dot_createdAt_le_date", nextDay);
			} catch (ParseException e) {
				System.out.println("获取时间异常");
			}
		}
		
		String geMoney = viewRequest.getParams().get("_and_w_dot_money_ge_number");
		if (geMoney != null && !geMoney.equals("")) {
			viewRequest.getParams().put("_and_w_dot_money_ge_number", Float.parseFloat(geMoney) * 100 + "");
		}
		String leMoney = viewRequest.getParams().get("_and_w_dot_money_le_number");
		if (leMoney != null && !leMoney.equals("")) {
			viewRequest.getParams().put("_and_w_dot_money_le_number", Float.parseFloat(leMoney) * 100 + "");
		}
		viewRequest.getSort().put("createdAt", "DESC");
		page = withdrawalsModel.findAll(viewRequest);
		return "withdrawals/index";
	}
	
	/**
	 * 提现成功
	 */
	public String withdrawalsSuccess(){
		loggingModel.info(logger,"提现成功,订单编号："+withdrawals.getCode()+"提现人id:"+withdrawals.getMember().getId());
		withdrawalsModel.updateWithdrawalsSuccess(id,withdrawals);
		return Render.json(new DialogBean("200", "提现成功","","closeCurrent"));
	}
	
	/**
	 * 提现驳回
	 */
	public String withdrawalsFail(){
		loggingModel.info(logger,"提现失败,订单编号："+withdrawals.getCode()+"提现人id:"+withdrawals.getMember().getId());
		withdrawalsModel.updateWithdrawalsFail(id,withdrawals);
		return Render.json(new DialogBean("200", "驳回提现请求","","closeCurrent"));
	}
	
	public String show() {
		page = withdrawalsModel.findAllByMemberId(viewRequest,id);
		return "withdrawals/dialog";
	}

	public String editNew() {
		// TODO Auto-generated method stub
		return null;
	}

	public String create() {
		// TODO Auto-generated method stub
		return null;
	}

	public String edit() {
		withdrawals = withdrawalsModel.getOne(id);
		return "withdrawals/edit";
	}

	public String update() {
		// TODO Auto-generated method stub
		return null;
	}

	public String destroy() {
		// TODO Auto-generated method stub
		return null;
	}

	public String destroyBatch() {
		// TODO Auto-generated method stub
		return null;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Page<Withdrawals> getPage() {
		return page;
	}

	public void setPage(Page<Withdrawals> page) {
		this.page = page;
	}

	public Withdrawals getWithdrawals() {
		return withdrawals;
	}

	public void setWithdrawals(Withdrawals withdrawals) {
		this.withdrawals = withdrawals;
	}

}
